package jpb.module.rest.member;

import jpb.constant.Reply;
import jpb.constant.ReturnState;
import jpb.constant.StatusCode;
import jpb.module.daomain.department.Department;
import jpb.module.daomain.member.Member;
import jpb.module.daomain.organize.Organize;
import jpb.module.daomain.profession.Profession;
import jpb.module.service.department.DepartmentService;
import jpb.module.service.member.MemberService;
import jpb.module.service.organize.OrganizeService;
import jpb.module.service.profession.ProfessionService;
import jpb.security.HandleCookie;
import jpb.security.HandleToken;
import jpb.util.RespUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by haol on 2017/4/11.
 */
@RestController
@RequestMapping("/api/app")
public class MemberAppController {
        @Autowired
        private MemberService memberService;
        @Autowired
        private OrganizeService organizeService;
        @Autowired
        private DepartmentService departmentService;
        @Autowired
        private ProfessionService professionService;

        HandleToken handleToken = new HandleToken();

        /**
         * @api {POST} /api/app/member/register 用户注册
         * @apiGroup MemberAPP
         * @apiName register
         * @apiDescription 组织成员注册
         * @apiParam {String} memberNum 编号(not null)
         * @apiParam {String} password 密码
         * @apiParamExample {json} 请求样例：
         * POST /api/app/member/register
         * {
         * "memberNum":"201430098009";
         * "password":"123",
         * }
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"注册成功"
         * }
         */
        @RequestMapping(value = "/member/register", method = RequestMethod.POST)
        public String register(String memberNum) {
                if (memberService.getLog(memberNum) != null) {
                        return new Reply(1, ReturnState.NO_UNIQUE).toJson();
                } else {
                        memberService.register(memberNum);
                        return new Reply(0, ReturnState.INSERT_SUCCESS).toJson();
                }
        }

        /**
         * @api {POST} /api/app/member 用户更新信息
         * @apiGroup MemberAPP
         * @apiName complete
         * @apiDescription 组织成员更新或完善
         * @apiParam {String} mobile 电话号码(可以为多个，至少一个)
         * @apiParam {String} weChat 微信号
         * @apiParam {String} site 联系地址
         * @apiParam {String} email 邮箱地址
         * @apiParam {String} newBirthday 新历生日
         * @apiParam {String} oldBirthday 旧历姓名
         * @apiParam {String} remark 备注
         * @apiParamExample {json} 请求样例：
         * POST /api/app/member
         * {
         * "id":"1",
         * "mobile":"17090077783";
         * "mobile":"18826274389";
         * "name":"张睿浩",
         * }
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":" 更新成功"
         * }
         */
        @RequestMapping(value = "/member", method = RequestMethod.POST)
        public String complete(Member member, HttpServletRequest request,HttpServletResponse response) {
                RespUtil.setACAO(response);
                String token = HandleCookie.getValueByName(request, StatusCode.COOKIE_JWT_APP);
                String num = handleToken.getValueByKey(token, StatusCode.SUBJECT_KEY);
                member.setMemberNum(num);
                if (memberService.get(num) == null){
                        memberService.complete(member);
                }else {
                        memberService.update(member);
                }
                return new Reply(0, ReturnState.UPDATE_SUCCESS).toJson();
        }

        /**
         * @api {GET} /api/app/exit/{oid}  用户退出组织
         * @apiGroup MemberAPP
         * @apiName exitOrg
         * @apiDescription 成员退出组织id为{oid}的组织
         * @apiParamExample {json} 请求样例：
         * GET /api/app/exit/organize_id
         * @apiSuccessExample {json} 返回样例:
         * {"code":0,"msg":"成功"}
         */
        @RequestMapping(value = "/exit/{oid}", method = RequestMethod.POST)
        public String exitOrg(@PathVariable String oid, HttpServletRequest request,HttpServletResponse response) {

                String token = HandleCookie.getValueByName(request, StatusCode.COOKIE_JWT_APP);
                String num = handleToken.getValueByKey(token, StatusCode.SUBJECT_KEY);

                organizeService.delMem(oid, num);
                return new Reply(0, ReturnState.SUCCESS).toJson();
        }

        /**
         * @api {GET} /api/app/member 获取用户信息
         * @apiGroup MemberAPP
         * @apiName getInfo
         * @apiDescription 查看个人详细信息，不包含组织信息
         * @apiParamExample {json} 请求样例：
         * GET /api/app/member
         * @apiSuccessExample {json} 返回样例:
         * {"code":0,"msg":"成功","data":{"name":"张睿浩","introduce":"我很帅","memberNum":"201430098009","createTime":"2017-05-12","updateTime":"2017-05-12","weChat":"police","mobiles":["17090077783","18826274389"],"site":"C15-232","email":"ruihaol@126.com","oldBirthday":"1993-11-12","newBirthday":"1993-03-22"}}
         */
        @RequestMapping(value = "/member", method = RequestMethod.GET)
        public String getInfo(HttpServletRequest request,HttpServletResponse response) {

                String token = HandleCookie.getValueByName(request, StatusCode.COOKIE_JWT_APP);
                String num = handleToken.getValueByKey(token, StatusCode.SUBJECT_KEY);

                Member member = memberService.get(num);
                RespUtil.setACAO(response);
                Reply<Member> reply = new Reply<Member>(0, ReturnState.SUCCESS, member);
                return reply.toJson();
        }

        /**
         * @api {GET} /api/app/member/{memberNum} 获取成员信息ByNum
         * @apiGroup MemberAPP
         * @apiName getByNum
         * @apiDescription 根据编号获取成员信息包含组织信息（部门，职称）
         * @apiParam {String} oid 组织id
         * @apiParam {String} memberNum 成员编号
         * @apiParamExample {json} 请求样例：
         * GET /api/app/member/201430098009?oid=organize_id
         * @apiSuccessExample {json} 返回样例:
         * {"code":0,"msg":"成功","data":{"oid":"c18d6dbc-3158-418e-829c-729c6c32ce9d","name":"张睿浩","introduce":"我很帅","memberNum":"201430098009","createTime":"2017-05-12","updateTime":"2017-05-12","weChat":"police","mobiles":["17090077783","18826274389"],"site":"C15-232","email":"ruihaol@126.com","oldBirthday":"1993-11-12","newBirthday":"1993-03-22","profession":{"id":16,"name":"学生","introduce":"不填写"}}}
         */
        @RequestMapping(value = "/member/{memberNum:.+}", method = RequestMethod.GET)
        public String getByNum(@PathVariable String memberNum,HttpServletResponse response,String oid) {
                Member member = memberService.getMsg(memberNum);
                Profession profession = professionService.getByOidAndMemberNum(oid,memberNum);
                Department department = departmentService.getByOidAndNum(oid,memberNum);
                RespUtil.setACAO(response);
                if (member == null){
                        member = new Member();
                        member.setMemberNum(memberNum);
                }
                member.setOid(oid);
                member.setProfession(profession);

                member.setDepartment(department);

                Reply<Member> reply = new Reply<Member>(0, ReturnState.SUCCESS, member);
                return reply.toJson();
        }

        /**
         * @api {GET} /api/app/organize/{oid} 获取组织信息ByOid
         * @apiGroup MemberAPP
         * @apiName getOrganize
         * @apiDescription 查看id为{oid}的组织详细信息
         * @apiParamExample {json} 请求样例：
         * GET /api/app/organize/i/organize_id
         * @apiSuccessExample {json} 返回样例:
         * {"code":0,"msg":"成功","data":{"id":"c18d6dbc-3158-418e-829c-729c6c32ce9d","mark":"gcu","name":"华@","tenureTime":"2006","createTime":"Tue Apr 18 22:05:44 CST 2017","updateTime":"Tue Apr 18 22:06:04 CST 2017","officeSite":"学校地址：广州市花都区学府路一号","officeTel":"招生咨询：020-66609166"}}
         */
        @RequestMapping(value = "/organize/{oid}", method = RequestMethod.GET)
        public String getOrganize(@PathVariable String oid) {
                Organize organize = organizeService.get(oid);
                Reply<Organize> reply = new Reply<Organize>(0, ReturnState.SUCCESS, organize);
                return reply.toJson();
        }

        /**
         * @api {GET} /api/app/department/{id} 获取部门信息ByDid
         * @apiGroup MemberAPP
         * @apiName getDepartment
         * @apiDescription 获取id为{id}的部门信息
         * @apiParamExample {json} 请求样例：
         * GET /api/app/department/department_id
         * @apiSuccessExample {json} 返回样例:
         * {"code":0,"msg":"成功","data":{"id":40,"name":"班群","parentId":36,"introduce":"不填写","child":[]}}
         */
        @RequestMapping(value = "/department/{id}", method = RequestMethod.GET)
        public String getDepartment(@PathVariable int id) {
                Department department = departmentService.get(id);
                Reply<Department> reply = new Reply<Department>(0, ReturnState.SUCCESS, department);
                return reply.toJson();
        }

        /**
         * @api {GET} /api/app/organize/mem-org  获取用户的组织信息
         * @apiGroup MemberAPP
         * @apiName getOrganizeByMid
         * @apiDescription 获取登录用户所加的所有组织列表及其信息
         * @apiParamExample {json} 请求样例：
         * GET /api/app/organize/mem-org
         * @apiSuccessExample {json} 返回样例:
         * {"code":0,"msg":"成功","data":[{"id":"c18d6dbc-3158-418e-829c-729c6c32ce9d","mark":"gcu","name":"华南理工大学广州学院","introduce":"@","tenureTime":"2006","createTime":"Tue Apr 18 22:05:44 CST 2017","updateTime":"Tue Apr 18 22:06:04 CST 2017","officeSite":"学校地址：广州市花都区学府路一号","officeTel":"招生咨询：020-66609166"}]}
         */
        @RequestMapping(value = "/organize/mem-org", method = RequestMethod.GET)
        public String getOrganizeByMid(HttpServletRequest request, HttpServletResponse response) {
                RespUtil.setACAO(response);

                String token = HandleCookie.getValueByName(request, StatusCode.COOKIE_JWT_APP);
                String num = handleToken.getValueByKey(token, StatusCode.SUBJECT_KEY);

                List<Organize> organizes = organizeService.getByNum(num);

                Reply<List<Organize>> reply =null;
                if (organizes.size() == 0) {
                       reply = new Reply<List<Organize>>(0, ReturnState.NO_DATA, organizes);
                }else {
                        reply = new Reply<List<Organize>>(0, ReturnState.SUCCESS, organizes);
                }

                return reply.toJson();
        }

        /**
         * @api {GET} /api/app/organize/{oid}/dep-tree 获取部门树ByOid
         * @apiGroup MemberAPP
         * @apiName getDepartmentTreeByOid
         * @apiDescription 获取id为{oid}的组织中的所有部门树结构
         * @apiParamExample {json} 请求样例：
         * GET /api/app/organize/{oid}/dep-tree
         * @apiSuccessExample {json} 返回样例:
         * {"code":0,"msg":"成功","data":[{"id":32,"name":"外国语学院","introduce":"@","child":[{"id":35,"name":"14英语1班","parentId":32,"introduce":"不填写","child":[]}],"icon":"icon-th"},{"id":33,"name":"管理学院","introduce":"@","child":[{"id":34,"name":"14会计1班","parentId":33,"introduce":"不填写","child":[]}],"icon":"icon-th"},{"id":31,"name":"@","child":[{"id":36,"name":"14软件1班","parentId":31,"introduce":"班主任：邵晓峰","child":[{"id":38,"name":"班干群","parentId":36,"introduce":"不填写","child":[]},{"id":39,"name":"团干群","parentId":36,"introduce":"不填写","child":[]},{"id":40,"name":"班群","parentId":36,"introduce":"不填写","child":[]}]},{"id":37,"name":"14软件2班","parentId":31,"introduce":"班主任：不知道","child":[]}],"icon":"icon-th"}]}
         */
        @RequestMapping(value = "/organize/{oid}/dep-tree", method = RequestMethod.GET)
        public String getDepartmentTreeByOid(@PathVariable String oid) {
                List<Department> departments = departmentService.getTree(oid);

                Reply<List<Department>> reply = new Reply<List<Department>>(0, ReturnState.SUCCESS, departments);
                return reply.toJson();
        }


        /**
         * @api {GET} /api/app/organize/{oid}/dep-leaf 获取部门叶子ByOid
         * @apiGroup MemberAPP
         * @apiName getDepartmentLeafByOid
         * @apiDescription 获取id为{oid}的组织中的叶子部门
         * @apiParamExample {json} 请求样例：
         * GET /api/app/organize/{oid}/dep-leaf
         * @apiSuccessExample {json} 返回样例:
         * {"code":0,"msg":"成功","data":[{"id":34,"name":"14会计1班","parentId":33,"introduce":"不填写","child":[]},{"id":35,"name":"14英语1班","parentId":32,"introduce":"不填写","child":[]},{"id":37,"name":"14软件2班","parentId":31,"introduce":"班主任：不知道","child":[]},{"id":38,"name":"班干群","parentId":36,"introduce":"不填写","child":[]},{"id":39,"name":"团干群","parentId":36,"introduce":"不填写","child":[]},{"id":40,"name":"班群","parentId":36,"introduce":"不填写","child":[]}]}
         */
        @RequestMapping(value = "/organize/{oid}/dep-leaf", method = RequestMethod.GET)
        public String getDepartmentLeafByOid(@PathVariable String oid) {
                List<Department> departments = departmentService.getLeafByOid(oid);

                Reply<List<Department>> reply = new Reply<List<Department>>(0, ReturnState.SUCCESS, departments);
                return reply.toJson();
        }

        /**
         * @api {GET} /api/app/member/{oid}/org 获取组织成员ByOid
         * @apiGroup MemberAPP
         * @apiName getOrganizeMember
         * @apiDescription 获取id为{oid}的组织中的所有成员基本信息，不包含除id为的其他组织信息
         * @apiParamExample {json} 请求样例：
         * GET /api/app/member/{oid}/org
         * @apiSuccessExample {json} 返回样例:
         * {"code":0,"msg":"成功","data":[{"memberNum":"201430098002","oid":"c18d6dbc-3158-418e-829c-729c6c32ce9d","did":40,"proName":"团员"},{"id":1,"name":"张睿浩","memberNum":"201430098009","createTime":"Thu Apr 20 16:55:35 CST 2017","updateTime":"Thu Apr 20 20:37:00 CST 2017","oid":"c18d6dbc-3158-418e-829c-729c6c32ce9d","did":40,"proName":"学生"},{"memberNum":"201430098017","oid":"c18d6dbc-3158-418e-829c-729c6c32ce9d","did":40,"proName":"学生"},{"memberNum":"201430098020","oid":"c18d6dbc-3158-418e-829c-729c6c32ce9d","did":40,"proName":"学生"},{"memberNum":"201430098022","oid":"c18d6dbc-3158-418e-829c-729c6c32ce9d","did":40,"proName":"学生"},{"memberNum":"201430098026","oid":"c18d6dbc-3158-418e-829c-729c6c32ce9d","did":40,"proName":"学生"},{"memberNum":"201430098166","oid":"c18d6dbc-3158-418e-829c-729c6c32ce9d","did":40,"proName":"学生"}]}
         */
        @RequestMapping(value = "/member/{oid}/org", method = RequestMethod.GET)
        public String getOrganizeMember(@PathVariable String oid, HttpServletResponse response) {
                List<Member> members = organizeService.getMem(oid);

                RespUtil.setACAO(response);

                Reply<List<Member>> reply = new Reply<List<Member>>(0, ReturnState.SUCCESS, members);
                return reply.toJson();
        }
        /**
         * @api {GET} /api/app/member/{did}/dep 获取组织成员ByDid
         * @apiGroup MemberAPP
         * @apiName getOrganizeMemberByDid
         * @apiDescription 获取id为{did}的部门中的所有成员信息，包含，职称，部门号
         * @apiParamExample {json} 请求样例：
         * GET /api/app/member/{did}/dep
         * @apiSuccessExample {json} 返回样例:
         * {"code":0,"msg":"成功","data":[{"memberNum":"201430098002","oid":"c18d6dbc-3158-418e-829c-729c6c32ce9d","did":40,"proName":"团员"},{"id":1,"name":"张睿浩","memberNum":"201430098009","createTime":"Thu Apr 20 16:55:35 CST 2017","updateTime":"Thu Apr 20 20:37:00 CST 2017","oid":"c18d6dbc-3158-418e-829c-729c6c32ce9d","did":40,"proName":"学生"},{"memberNum":"201430098017","oid":"c18d6dbc-3158-418e-829c-729c6c32ce9d","did":40,"proName":"学生"},{"memberNum":"201430098020","oid":"c18d6dbc-3158-418e-829c-729c6c32ce9d","did":40,"proName":"学生"},{"memberNum":"201430098022","oid":"c18d6dbc-3158-418e-829c-729c6c32ce9d","did":40,"proName":"学生"},{"memberNum":"201430098026","oid":"c18d6dbc-3158-418e-829c-729c6c32ce9d","did":40,"proName":"学生"},{"memberNum":"201430098166","oid":"c18d6dbc-3158-418e-829c-729c6c32ce9d","did":40,"proName":"学生"}]}
         */
        @RequestMapping(value = "/member/{did}/dep", method = RequestMethod.GET)
        public String getOrganizeMemberByDid(@PathVariable Integer did, HttpServletResponse response) {
                List<Member> members = departmentService.getMem(did);
                RespUtil.setACAO(response);
                Reply<List<Member>> reply = new Reply<List<Member>>(0, ReturnState.SUCCESS, members);
                return reply.toJson();
        }


        /**
         * @api {GET} /api/app/profession 获取职称信息
         * @apiGroup MemberAPP
         * @apiName getProfession
         * @apiDescription 根据组织id和职称名获取职称的详细信息
         *  @apiParam {String} oid 组织id
         *   @apiParam {String} name 职称名
         * @apiParamExample {json} 请求样例：
         * GET /api/app/profession
         * @apiSuccessExample {json} 返回样例:
         * {"code":0,"msg":"成功","data":{"id":15,"name":"团员","introduce":"不填写"}}
         */
        @RequestMapping(value = "/profession", method = RequestMethod.GET)
        public String getProfession(String oid, String name) {
                Profession profession = professionService.get(oid, name);
                Reply<Profession> reply = new Reply<Profession>(0, ReturnState.SUCCESS, profession);
                return reply.toJson();
        }

        /**
         * @api {GET} /api/app/member/haveMsg 获取用户是否完善信息
         * @apiGroup MemberAPP
         * @apiName haveMsg
         * @apiDescription 根据Cookie查找该用户是否完善了信息
         * @apiParamExample {json} 请求样例：
         * GET /api/app/member/haveMsg
         * @apiSuccessExample {json} 返回样例:
         * {"true"}
         */
        @RequestMapping(value ="/member/haveMsg" ,method = RequestMethod.GET)
        public boolean haveMsg(HttpServletRequest request,HttpServletResponse response){
                RespUtil.setACAO(response);
                String token = HandleCookie.getValueByName(request, StatusCode.COOKIE_JWT_APP);
                String num = handleToken.getValueByKey(token, StatusCode.SUBJECT_KEY);
                return memberService.get(num) != null;
        }




}
